﻿using Oraycn.MCapture;
using Oraycn.MFile;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;


namespace EBQComm
{
    /// <summary>
    /// 屏幕录像机
    /// </summary>
    public class ScreenRecorderHelper
    {
        /// <summary>
        /// 图形采集器
        /// </summary>
        private ICapturer capturer;
        /// <summary>
        /// 视频处理器
        /// </summary>
        private VideoFileMaker maker;
        /// <summary>
        /// 录像路径
        /// </summary>
        public string recordsPath { get; set; }
        /// <summary>
        /// 目标文件
        /// </summary>
        public string targetFile { get; set; }
        /// <summary>
        /// 是否在录制状态
        /// </summary>
        private bool recording { get; set; }
        /// <summary>
        /// 帧率
        /// </summary>
        private int fps { get; set; }

        private double width { get; set; }

        private double height { get; set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="recordsPath">录像保存路径</param>
        public ScreenRecorderHelper(string recordsPath, double width, double height)
        {
            this.recordsPath = recordsPath;
            if (!Directory.Exists(recordsPath))
            {
                Directory.CreateDirectory(recordsPath);
            }
            this.recording = false;
            this.fps = 10;
            if (width % 4 != 0)
            {
                width -= width % 4;
            }
            this.width = width;
            if (height % 4 != 0)
            {
                height -= height % 4;
            }
            this.width = width;
            this.height = height;
            Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("mapu", "mapu0121");//免费用户
            Oraycn.MFile.GlobalUtil.SetAuthorizedUser("mapu", "mapu0121");//免费用户
        }

        public ScreenRecorderHelper(string recordsPath, int fps, double width, double height)
        {

            this.recordsPath = recordsPath;
            //如果不存在录像目录则场景
            if (!Directory.Exists(recordsPath))
            {
                Directory.CreateDirectory(recordsPath);
            }
            this.recording = false;
            this.fps = fps;
            if (width % 4 != 0)
            {
                width -= width % 4;
            }
            this.width = width;
            if (height % 4 != 0)
            {
                height -= height % 4;
            }
            this.height = height;
            Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("mapu", "mapu0121");//免费用户
            Oraycn.MFile.GlobalUtil.SetAuthorizedUser("mapu", "mapu0121");//免费用户
            this.capturer = CapturerFactory.CreateDesktopCapturer(this.fps, true);//创建一个屏幕采集器,10帧每秒&显示光标
            ((IDesktopCapturer)this.capturer).ImageCaptured += new ESBasic.CbGeneric<Bitmap>(this.AddImageToVedio);//添加一个图像处理器
        }

        public void start()
        {
            this.capturer = CapturerFactory.CreateDesktopCapturer(this.fps, true);//创建一个屏幕采集器,10帧每秒&显示光标

            this.maker = new VideoFileMaker();
            this.maker.AutoDisposeVideoFrame = true;
            
            ((IDesktopCapturer)this.capturer).ImageCaptured += new ESBasic.CbGeneric<Bitmap>(this.AddImageToVedio);//添加一个图像处理器
            this.capturer.Start();
            this.targetFile = recordsPath + "/" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".mp4";
            maker.AutoDisposeVideoFrame = true;
            this.recording = true;
            this.maker.Initialize(targetFile, VideoCodecType.H264, (int)this.width, (int)this.height, this.fps, AudioCodecType.AAC, 16000, 1, true);//初始化一个视频压缩器
        }

        public void stop()
        {
            this.recording = false;
            this.capturer.Stop();
            this.maker.Close(true);
            this.maker.Dispose();
        }

        /// <summary>
        /// 图像处理器
        /// </summary>
        /// <param name="image">图像</param>
        private void AddImageToVedio(Bitmap image)
        {
            //增加水纹
            Graphics gBitmap = Graphics.FromImage(image);
            SolidBrush drawBrush = new SolidBrush(Color.Red);
            Font drawFont = new Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Pixel);
            int xPos = image.Width - 325;
            int yPos = image.Height - 45;
            gBitmap.DrawString("易保全取证时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"), drawFont, drawBrush, xPos, yPos);
            maker.AddVideoFrame(image);//添加图像到视频

        }
    }
}
